package com.chookss.home.audit.project;

import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.chookss.R;
import com.chookss.Urls;
import com.chookss.home.entity.GroupEntity;
import com.chookss.tools.ClearMoreZeroUtil;
import com.chookss.tools.MyEvent;
import com.chookss.tools.MyToast;
import com.chookss.tools.OnlyOneEditText;
import com.chookss.tools.SignUtils2;
import com.chookss.tools.Utils;
import com.chookss.tools.okgoconfig.MyHttpRequest;
import com.chookss.tools.okgoconfig.OkGoUtil;
import com.chookss.tools.okgoconfig.callback.JsonCallback;
import com.lvgroup.hospital.base.BaseAct;
import com.lzy.okgo.model.Response;

import org.greenrobot.eventbus.EventBus;
import org.jetbrains.annotations.Nullable;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

//项目预算
public class GroupBudgetActivity extends BaseAct {
    @BindView(R.id.common_title_txt)
    TextView commonTitleTxt;
    @BindView(R.id.tvSure)
    TextView tvSure;
    @BindView(R.id.edMoney)
    EditText edMoney;
    @BindView(R.id.edPeopleMoney)
    EditText edPeopleMoney;
    @BindView(R.id.edReimbursement)
    EditText edReimbursement;
    @BindView(R.id.edService)
    EditText edService;
    @BindView(R.id.edShop)
    EditText edShop;
    private GroupEntity groupEntity;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group_budget);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        initState();
        edMoney.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_DECIMAL);
        edMoney.addTextChangedListener(new OnlyOneEditText(edMoney).setInputNum(2));
//        edPeopleMoney.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_DECIMAL);
//        edPeopleMoney.addTextChangedListener(new OnlyOneEditText(edPeopleMoney).setInputNum(2));
        edReimbursement.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_DECIMAL);
        edReimbursement.addTextChangedListener(new OnlyOneEditText(edReimbursement).setInputNum(2));
        edService.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_DECIMAL);
        edService.addTextChangedListener(new OnlyOneEditText(edService).setInputNum(2));
        groupEntity= (GroupEntity) getIntent().getSerializableExtra("groupEntity");
        commonTitleTxt.setText("项目预算");
        if(!Utils.isNull(groupEntity.getYsAmtTotal())){
            tvSure.setText("修改");
            edMoney.setText(ClearMoreZeroUtil.subZeroAndDot(groupEntity.getYsAmtTotal()));
            edReimbursement.setText(ClearMoreZeroUtil.subZeroAndDot(groupEntity.getYsAmtBaoxiao()));
//            edPeopleMoney.setText(ClearMoreZeroUtil.subZeroAndDot(groupEntity.getYsAmtEmp()));
            edService.setText(ClearMoreZeroUtil.subZeroAndDot(groupEntity.getYsAmtEmpDaka()));
            edShop.setText(ClearMoreZeroUtil.subZeroAndDot(groupEntity.getYsGoodsNum()));
        }
    }

    private void uploadData() {
        Map<String, String> map = new HashMap<>();
        showLoading();
        map.put("ysAmtTotal",edMoney.getText().toString());
        map.put("ysAmtBaoxiao",edReimbursement.getText().toString());
        map.put("ysAmtEmp","1000000");
        map.put("ysAmtEmpDaka",edService.getText().toString());
        map.put("ysGoodsNum",edShop.getText().toString());
        map.put("groupCode",groupEntity.getGroupCode());
        MyHttpRequest.postRequest(Urls.updateItemGroupByGroupCode, this, SignUtils2.makeSignStr(map), new JsonCallback<String>() {
            @Override
            public void onSuccess(Response<String> response) {
                try {
                    dismissLoading();
                    JSONObject jsonObject = new JSONObject(response.body().toString());
                    if ("200".equals(jsonObject.getString("code"))) {
                        EventBus.getDefault().post(new MyEvent("RefreshGroupRecord"));
                        MyToast.show("提交成功");
                        finish();
                    } else {
                        if (jsonObject.getString("code").startsWith("400")) {
                            OkGoUtil.onErrorIntent(jsonObject.getString("code"), jsonObject.getString("msg"));
                        } else {
                            MyToast.show(jsonObject.getString("msg"));
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    dismissLoading();
                    MyToast.show("提交失败，请稍后重试");
                }
            }
        });
    }

    @OnClick({R.id.common_title_back, R.id.tvSure})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.common_title_back:
                finish();
                break;
            case R.id.tvSure:
                if (Utils.isNull(groupEntity.getGroupCode())) {
                    MyToast.show("项目组为空，请先联系管理员配置");
                    return;
                }
                if (Utils.isNull(edMoney.getText().toString())) {
                    MyToast.show("请输入项目金额");
                    return;
                }
//                if (Utils.isNull(edPeopleMoney.getText().toString())) {
//                    MyToast.show("请输入人力成本");
//                    return;
//                }
                if (Utils.isNull(edReimbursement.getText().toString())) {
                    MyToast.show("请输入项目开支");
                    return;
                }
                if (Utils.isNull(edService.getText().toString())) {
                    MyToast.show("请输入人力服务");
                    return;
                }
                if (Utils.isNull(edShop.getText().toString())) {
                    MyToast.show("请输入物料数量");
                    return;
                }
                uploadData();
                break;
        }
    }
}
